package com.example.tourstor;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DisplayActivity extends AppCompatActivity {

    EditText mudidi, chufa, feiyong, dianhua,renshu;
    Button tj;
    String  mdd, cf, fy, dh,rs;
    int UID,repeat;
    Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display);

        Intent intent = getIntent();
        //UID = intent.getStringExtra("U_id");
        UID=2;

        mudidi = findViewById(R.id.mudidi_display);
        chufa = findViewById(R.id.chufa_display);
        feiyong = findViewById(R.id.feiyong_display);
        dianhua = findViewById(R.id.dianhua_display);
        renshu = findViewById(R.id.renshu_display);
        tj = findViewById(R.id.tijiao);

        tj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mdd = mudidi.getText().toString();
                cf = chufa.getText().toString();
                fy = feiyong.getText().toString();
                dh = dianhua.getText().toString();
                rs = renshu.getText().toString();
                if (mdd.equals("") || cf.equals("") || fy.equals("") || dh.equals("")||rs.equals("")){
                    Toast.makeText(DisplayActivity.this, "请输入完整信息", Toast.LENGTH_LONG).show();
                } else {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                java.sql.Connection cn = DBOpenHelper.getConn();
                                String sql = "select * from team where U_id=?,setOutTime=?,destination=?,cost=?,tell=?,number=?";
                                PreparedStatement st = cn.prepareStatement(sql);
                                st.setInt(1, UID);
                                st.setString(1, cf);
                                st.setString(1, mdd);
                                st.setString(1, fy);
                                st.setString(1, dh);
                                st.setString(1, rs);
                                ResultSet rs = st.executeQuery();
                                if (rs != null) {
                                    if (rs.next()) {
                                        repeat = 1;
                                    } else {
                                        repeat = 0;
                                    }
                                }
                                //DBOpenHelper.closeAll(cn, st);
                            } catch (SQLException e) {
                                e.printStackTrace();
                            }

                            Message msg = handler.obtainMessage(5);
                            msg.obj = repeat;
                            handler.sendMessage(msg);
                        }
                    }).start();

                    handler = new Handler(){
                        @Override
                        public void handleMessage(Message msg){
                            if  (msg.what == 5){
                                repeat = (int) msg.obj;
                                if (repeat == 1){
                                    Toast.makeText(DisplayActivity.this, "您已发布过相同组团信息", Toast.LENGTH_SHORT).show();
                                }else {
                                    new Thread(new Runnable() {
                                        @Override
                                        public void run() {
                                            try {
                                                java.sql.Connection cn = DBOpenHelper.getConn();
                                                String sql = "insert into team(U_id,setOutTime,destination,cost,tell,number) values(?,?,?,?,?,?)";
                                                PreparedStatement st = cn.prepareStatement(sql);
                                                st.setInt(1, UID);
                                                st.setString(2, cf);
                                                st.setString(3, mdd);
                                                st.setString(4, fy);
                                                st.setString(5, dh);
                                                st.setString(6, rs);
                                                st.executeUpdate();
                                            } catch (SQLException e) {
                                                e.printStackTrace();
                                            }
                                        }
                                    }).start();
                                    showDialog();
                                }
                            }
                        }
                    };
                }
            }
        });
    }

    public void showDialog(){
        AlertDialog.Builder builder =new AlertDialog.Builder(this);
        builder.setTitle("发布成功");
        builder.setMessage("您的组团消息已发布");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Intent intent = new Intent(DisplayActivity.this,CheckActivity.class);
                intent.putExtra("U_id",UID);
                startActivity(intent);
            }
        });
        AlertDialog dialog=builder.create();
        dialog.show();
    }
}